package com.eorchis.module.classapplydep.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.basedao.dao.impl.HibernateAbstractBaseDao;
import com.eorchis.module.classapplydep.dao.IClassApplyDepDao;
import com.eorchis.module.classapplydep.domain.ClassApplyDep;
import com.eorchis.module.classapplydep.domain.ClassApplyDepBean;

import com.eorchis.core.basedao.query.condition.IPagingQueryCondition;
import com.eorchis.core.basedao.query.condition.builder.CompareType;
import com.eorchis.core.basedao.query.condition.builder.IConditionBuilder;
import com.eorchis.core.basedao.query.condition.builder.impl.DefaultQueryConditionBuilder;
import com.eorchis.core.basedao.query.mapper.MapperType;
import com.eorchis.core.basedao.query.mapper.ResultMapperFactory;
import com.eorchis.core.basedao.query.mapper.impl.BeanResultMapper;

import com.eorchis.core.page.commond.SortInfoBean;
import com.eorchis.core.ui.commond.IQueryCommond;

import com.eorchis.module.classapplydep.ui.commond.ClassApplyDepQueryCommond;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: ClassApplyDepDaoImpl<br>
 * Description: 培训班-单位关系数据接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author HuangTF
 * @createDate 2013-12-18
 * @version $Revision$
 */
@SuppressWarnings("unchecked")
@Repository(value = "main.java.com.eorchis.module.classapplydep.dao.impl.ClassApplyDepDaoImpl")
public class ClassApplyDepDaoImpl extends HibernateAbstractBaseDao implements IClassApplyDepDao {
	
	@Override
	public Class<? extends IBaseEntity> entityClass() {
		return ClassApplyDep.class;
	}

	@Override
	public void queryConditionProcessor(
			IConditionBuilder defaultQueryConditionBuilder,
			IQueryCommond queryCommond) {
		ClassApplyDepQueryCommond classApplyDepQueryCommond = (ClassApplyDepQueryCommond)queryCommond;
		defaultQueryConditionBuilder.setBaseQueryString("select cad.classApplydepId as classApplydepId,cad.department.deptID as deptID," +
															"cad.trainingClass.classId as classId," +
															"cad.department.deptName as deptName,cad.restrictPersonNum as restrictPersonNum " +
																"from ClassApplyDep as cad");
		if(PropertyUtil.objectNotEmpty(classApplyDepQueryCommond.getClassId()))
		{
			defaultQueryConditionBuilder.addCondition("cad.trainingClass.classId", CompareType.EQUAL, classApplyDepQueryCommond.getClassId());
		}
		if(PropertyUtil.objectNotEmpty(classApplyDepQueryCommond.getDepName()))
		{
			defaultQueryConditionBuilder.addCondition("cad.department.deptName", CompareType.LIKE, classApplyDepQueryCommond.getDepName().replace(" ", ""));
		}
		defaultQueryConditionBuilder.setQueryStringType(QueryStringType.HQL);
		defaultQueryConditionBuilder.setResultObject(ClassApplyDepBean.class);
	}
	
	/**
	 * 查询未选单位列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-18
	 */
	@Override
	public List<ClassApplyDepBean> findNoSelectedDepList(ClassApplyDepQueryCommond queryCommond)
			throws Exception
	{	
		StringBuffer hql=new StringBuffer("select d.deptID as deptID,d.deptName as deptName from Department d " +
			"where d.deptID not in(select cad.department.deptID from ClassApplyDep cad where cad.trainingClass.classId = :classId)");
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("classId", queryCommond.getClassId());
		if(PropertyUtil.objectNotEmpty(queryCommond.getParentId()))
		{
			hql.append(" and d.parentID = :parentID");
			params.put("parentID", queryCommond.getParentId());
		}
		if(PropertyUtil.objectNotEmpty(queryCommond.getDepName()))
		{
			hql.append(" and d.deptName like :deptName");
			params.put("deptName", "%"+queryCommond.getDepName().replace(" ", "")+"%");
		}
		
		// 增加排序
		if (queryCommond.getSortInfos() != null) 
		{
			SortInfoBean sortInfoBean = queryCommond.getSortInfos();
			if (PropertyUtil.objectNotEmpty(sortInfoBean.getProperty())) 
			{
				hql.append(" order by " + sortInfoBean.getProperty().trim());
				if (PropertyUtil.objectNotEmpty(sortInfoBean.getDirection())) 
				{
					hql.append(" " + sortInfoBean.getDirection().trim());
				}
			}
		} 
		IConditionBuilder defaultQueryConditionBuilder  = new DefaultQueryConditionBuilder();
		defaultQueryConditionBuilder.setBaseQueryString(hql.toString());
		defaultQueryConditionBuilder.setQueryStringType(QueryStringType.HQL);
		IPagingQueryCondition queryCondition = (IPagingQueryCondition) defaultQueryConditionBuilder.buliderQueryCondition();
		queryCondition.setParameters(params);
		Long count = this.count(queryCondition);
		List<ClassApplyDepBean> findList = new ArrayList<ClassApplyDepBean>();
		
		BeanResultMapper mapper = (BeanResultMapper) ResultMapperFactory.getInstance().getMapper(MapperType.Bean);
		mapper.setResultClass(ClassApplyDepBean.class);
		queryCondition.setResultMapper(mapper);
		
		if(queryCommond.getPage()==-1)
		{
			 findList = (List<ClassApplyDepBean>) this.findList(queryCondition);
		}
		else
		{
			int firstResult = queryCommond.calculate(count.longValue());
			 findList = (List<ClassApplyDepBean>) this.findList(queryCondition,firstResult, queryCommond.getLimit());
		}
		return findList;
	}
	/**
	 * 更新已选单位限制人数
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-19
	 */
	public void updatePersonNum(ClassApplyDepQueryCommond queryCommond)
			throws Exception
	{
		String hql="update ClassApplyDep cad set cad.restrictPersonNum=:restrictPersonNum where cad.classApplydepId = :classApplydepId";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("restrictPersonNum", queryCommond.getSearchRestrictPersonNum());
		params.put("classApplydepId", queryCommond.getSearchClassApplydepId());
		this.executeUpdate(QueryStringType.HQL, hql, params);
	}
	/**
	 * 更新已选单位限制人数
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2014-1-23
	 */
	public void updateRestrictPersonNum(ClassApplyDepQueryCommond queryCommond,Long restrictPersonNum)
			throws Exception
	{
		String hql="update ClassApplyDep cad set cad.restrictPersonNum=:restrictPersonNum where cad.trainingClass.classId = :classId and cad.department.deptID =:deptID";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("restrictPersonNum", restrictPersonNum);
		params.put("classId", queryCommond.getClassId());
		params.put("deptID", queryCommond.getDepId());
		this.executeUpdate(QueryStringType.HQL, hql, params);
	}
	/**
	 * 获取已添加单位总人数
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-19
	 */
	public Long findPersonNum(ClassApplyDepQueryCommond queryCommond)
			throws Exception
	{
		String hql="select sum(cad.restrictPersonNum) from ClassApplyDep cad where cad.trainingClass.classId = :classId";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("classId", queryCommond.getClassId());
		List<?> list=(List<?>) this.executeFind(QueryStringType.HQL, hql, params);
		Long num=(Long) list.get(0);
		return num;
	}
}
